# -*- UTF-8 -*-
from elasticsearch import Elasticsearch


class ESService:
    def __init__(self, url="http://10.1.1.9:9266"):
        self._es = Elasticsearch([url])

    """
    :return [{'Title':'Title1', 'Body':'Body1'}, {'Title':'Title2', 'Body':'Body2'}, ...], 
    """
    def search(self, title="", body="", size=50):
        doc = {
            "query": {
                "bool": {
                    "should": [
                        {"match": {"Title": title}},
                        {"match": {"Body": body}}
                    ]
                }
            }
        }

        results = self._es.search(index="question", doc_type="question", body=doc, size=size)
        print(results)
        for res in results['hits']['hits']:
            print(res['_source'])

if __name__ == '__main__':
    es = ESService()
    es.search(title="java", size=10)